﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;
using Microsoft.Expression.Encoder;
using System.Globalization;

namespace VideoEncoder
{
    [ValueConversion(typeof(object), typeof(string))]
    public class ShowProgressStatusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            ItemStatus status = (ItemStatus)value;
            if(status == ItemStatus.Encoding)
                return Visibility.Visible;
            else
                return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            // we don't intend this to ever be called
            return null;
        }
    }
    [ValueConversion(typeof(object), typeof(string))]
    public class ShowStatusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            ItemStatus status = (ItemStatus)value;
            if (status == ItemStatus.Encoding)
                return Visibility.Collapsed;
            else
                return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            // we don't intend this to ever be called
            return null;
        }
    }

    [ValueConversion(typeof(object), typeof(string))]
    public class StatusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            ItemStatus status = (ItemStatus)value;
            switch (status)
            {
                case ItemStatus.FinishedEncode:
                    return "Finished";
                case ItemStatus.WaitingToEncode:
                    return "Waiting";
                default:
                    return status.ToString();
            }
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            // we don't intend this to ever be called
            return null;
        }
    }

    [ValueConversion(typeof(object), typeof(string))]
    public class StatusIsEnablesConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            ItemStatus status = (ItemStatus)value;
            if (status == ItemStatus.Encoding)
                return false;
            else
                return true;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            // we don't intend this to ever be called
            return null;
        }
    }

    [ValueConversion(typeof(object), typeof(string))]
    public class ShowUpDownConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
      object parameter, CultureInfo culture)
        {
            MediaEncoder item = value as MediaEncoder;
            if (item == null || MainWindow.Default == null || MainWindow.Default.Queue == null)
                return false;
            int index = MainWindow.Default.Queue.IndexOf(item);
            if (parameter.ToString() == "Up" && index > 0)
                return true;
            else if (parameter.ToString() == "Down" && index < MainWindow.Default.Queue.Count - 1)
                return true;
            return false;
        }

        public object ConvertBack(object value, Type targetType,
      object parameter, CultureInfo culture)
        {
            // we don't intend this to ever be called
            return null;
        }
    }

    public class ByteConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string size = "0 KB";

            if (value != null)
            {
                double byteCount = System.Convert.ToDouble(value);

                if (byteCount >= 1073741824)
                    size = String.Format("{0:##.##}", byteCount / 1073741824) + " GB";
                else if (byteCount >= 1048576)
                    size = String.Format("{0:##.##}", byteCount / 1048576) + " MB";
                else if (byteCount >= 1024)
                    size = String.Format("{0:##.##}", byteCount / 1024) + " KB";
                else if (byteCount > 0 && byteCount < 1024)
                    size = "1 KB";    //Bytes are unimportant ;)            

            }

            return size;
        }

        //We only use one-way binding, so we don't implement this.
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    [ValueConversion(typeof(object), typeof(string))]
    public class BitRateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            int bitrate = (int)value;
            return bitrate / 1000;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                int bitrate = System.Convert.ToInt32(value);
                return bitrate * 1000;
            }
            catch
            {
                return 0;
            }
        }
    }
}
